/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.absisgroup.nt;

import com.absisgroup.nsc.utilities.GenericServlet;
import com.absisgroup.nsc.utilities.Util;
import java.math.BigDecimal;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Calendar;
import java.util.Properties;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.json.JSONArray;
import org.json.JSONObject;

/**
 *
 * @author Hue
 */
public class XemPhieuNhapKho extends GenericServlet {

    @Override
    public int process(final Connection con, final HttpServletRequest request, final HttpServletResponse response) throws Exception {
        BigDecimal impProdPK = Util.toBigDecimal(request.getParameter("impProdPK"));

        writeToResponse(response, toJSONObject(con, impProdPK));

        return DONT_RUN_JSP;
    }

    private String toJSONObject(final Connection con, BigDecimal impProdPK) throws Exception {
        final JSONObject jsObj = new JSONObject();
        
        String strQuery = "select IMP_PROD_DATE, WH_NAME, IMP_PROD_FROM_WH_NAME, IMP_PROD_FROM_WH_ADDRESS, IMP_PROD_FROM_WH_PHONE, IMP_PROD_COMMENT, IMP_PROD_NUMBER, IMP_PROD_NO " + 
                          "from IMPORT_PRODUCTS, WAREHOUSE where IMP_PROD_TO_WH_FK = WH_PK and IMP_PROD_PK = ?";
        PreparedStatement pStmt = con.prepareStatement(strQuery);
        pStmt.setBigDecimal(1, impProdPK);
        ResultSet rs = pStmt.executeQuery();
        if(rs.next()) {
            Properties texts = Util.getAppTexts(getServletContext());
            String dateText = texts.getProperty("NGAY_THANG_NAM");
            Calendar cal = Calendar.getInstance();
            cal.setTime(rs.getDate("IMP_PROD_DATE"));
            dateText = Util.replace(dateText, "{0}", String.valueOf(cal.get(Calendar.DATE)));
            dateText = Util.replace(dateText, "{1}", String.valueOf(cal.get(Calendar.MONTH) + 1));
            dateText = Util.replace(dateText, "{2}", String.valueOf(cal.get(Calendar.YEAR)));

            jsObj.put("dateText", dateText);
            jsObj.put("whName", rs.getString("IMP_PROD_FROM_WH_NAME"));
            jsObj.put("whAddress", rs.getString("IMP_PROD_FROM_WH_ADDRESS"));
            jsObj.put("whPhone", rs.getString("IMP_PROD_FROM_WH_PHONE"));
            jsObj.put("comment", rs.getString("IMP_PROD_COMMENT"));
            jsObj.put("toWhName", rs.getString("WH_NAME"));
            jsObj.put("impProdNo", Util.replace(texts.getProperty("SO_PHIEU"), "{0}", rs.getString("IMP_PROD_NO")));
        }
        pStmt.close();

        JSONArray records = getDetails(con, impProdPK);

        jsObj.put("records", records);
        if(impProdPK != null) {
            jsObj.put("orderPK", impProdPK.longValue());
        }
        jsObj.put("success", true);

        return jsObj.toString();
    }
    
    private JSONArray getDetails(Connection con, BigDecimal impProdPK) throws Exception {
        JSONArray records = new JSONArray();
        String strQuery = "select IMP_PROD_DET_STT, PROD_CODE, PROD_FOR_SELL, IMP_PROD_DET_SO_KG, IMP_PROD_DET_SO_MET, IMP_PROD_DET_DON_GIA " + 
                          "from IMP_PROD_DETAILS, PRODUCTS " +
                          "where PROD_PK = IMP_PROD_DET_PROD_FK and IMP_PROD_DET_IMP_PROD_FK = ? order by IMP_PROD_DET_STT";
        PreparedStatement pStmt = con.prepareStatement(strQuery);
        pStmt.setBigDecimal(1, impProdPK);
        int iiIndex = 1;
        ResultSet rs = pStmt.executeQuery();
        while(rs.next()) {
            JSONObject jsObj = new JSONObject();
            jsObj.put("stt", iiIndex++);
            jsObj.put("prodCode", rs.getString("PROD_CODE"));
            jsObj.put("tenMH", rs.getString("PROD_FOR_SELL"));
            BigDecimal soKg = rs.getBigDecimal("IMP_PROD_DET_SO_KG");
            BigDecimal soMet = rs.getBigDecimal("IMP_PROD_DET_SO_MET");
            jsObj.put("soKg", soKg);
            jsObj.put("soMet", soMet);
            BigDecimal donGia = rs.getBigDecimal("IMP_PROD_DET_DON_GIA");
            jsObj.put("donGiaNhap", donGia);
            if(soKg != null) {
                if(soMet != null && soMet.doubleValue() != 0) {
                    jsObj.put("soKgm", soKg.divide(soMet, 3, BigDecimal.ROUND_HALF_UP));
                }
                if(donGia != null) {
                    jsObj.put("thanhTien", soKg.multiply(donGia));
                }
            }
            records.put(jsObj);
        }
        pStmt.close();
        
        return records;
    }
    
    @Override
    protected boolean useConnection() {
        return true;
    }
}
